package com.junjie.web.action.user;

import java.util.Date;

import com.junjie.dao.impl.UserDaoImpl;
import com.junjie.entity.User;
import com.junjie.util.EmailUtil;
import com.junjie.util.MD5Util;
import com.junjie.util.ValidateCode;
import com.junjie.web.action.BaseAction;
/**
 * 只定义这一个action,其余的按照动态生成action的方式,在struts.xml文件中配置
 * @author Administrator
 *
 */
public class UserAction extends BaseAction{
	private String emailAddress;
	private User user;
	private UserDaoImpl dao = new UserDaoImpl();
	private boolean errorEmailAddress;//填写email地址的错误提示信息
	private String codeStr;//用户输入的验证码
	private String emailCode;//用户输入的邮箱激活码
	private String errorInputEmailCode;
	
	/**
	 * 默认的方法,点击免费注册的时候连接此方法
	 */
	public String execute(){
		
		return "to_regiest";
	}
	/**
	 * 进入注册页面加载验证码
	 */
	public String showCode(){
		//进入注册页面时加载验证码
		ValidateCode checkCode = new ValidateCode();
		try {
			is = checkCode.getCode(session);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "showImageCode";
	}
	/**
	 * 异步验证邮箱地址是否已经注册过
	 * @return
	 */
	public String checkEmail(){
		
		try {
		int	i = dao.findByEmailAdd(emailAddress);
		if(i>0){
			//此邮箱地址已经被注册
			errorEmailAddress = false;
		}else{
			errorEmailAddress = true;
		}
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "checkemail";
	}
	
	/**
	 * 注册验证,点击提交注册时候触发的方法
	 * @return
	 */
	public String checkLogin(){
		//获取页面的验证码,根据用户输入的验证码和生成的验证码
		//比较是否一致
		String code = (String)session.get("imageCode");
		if(code.equalsIgnoreCase(codeStr)){
			//输入的和生成的一致,验证成功
			//生成邮箱激活码
			String emailCode = EmailUtil.createEmailCode();
			try {
			//对接收到的密码进行MD5验证
			user.setPassword(MD5Util.md5(user.getPassword()));
			user.setEmailVerifyCode(emailCode);
			user.setEmailVerify(false);
			dao.addUser(user);
			return "verifyForm";
			} catch (Exception e) {
				e.printStackTrace();
				return "error";
			}
		}
		return "to_regiest";
		
	}
	/**
	 * 点击完成按钮 完成注册 ,进入提示成功注册的页面(register_ok.jsp)
	 * @return
	 */
	public String regiestOk(){
		try {
			//接收用户输入的激活码
			//根据用户输入的激活码查询用户信息
			User u = dao.findByEmailCode(emailCode);
			if(u !=null){
				//找到符合激活码要求的用户,该用户激活成功,更新数据库中的(is_email_verify)的信息
				dao.changVerify(u.getId());
				//将用户保存到session中用来作为当前的登录会员用户.
				session.put("loginUser", u);
				return "okRegiest";
			}else{
				//否则返回到激活界面(verify_form.jsp)
				//提示邮箱中的激活码和用户输入的不一致,请更改后激活
				errorInputEmailCode = "输入不正确,请重新填写";
				return "verifyForm";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	/**
	 * 注册成功用户先不激活账号,点击稍后激活按钮
	 * @return
	 */
	public String afterActivation(){
		
		return "to_mainPage";
	}
//-----------------------------------------------------------------------登录操作-----------------
	/**
	 * 退出登录
	 */
	public String exit(){
		//将浏览器中的session全部删除
		session.clear();
		return "to_mainPage";
	}
	/**
	 * 用户登录,点击主页面的登录按钮
	 * 1.把数据库中用户最后登录时间添加上当前时间
	 */
	public String toLogin(){
		
		return "to_login";
	}
	/**
	 * 验证用户输入的邮箱和密码,成功则跳转到住页面,不成功则提示注册
	 * @return
	 */
	public String doLogin(){
		try {
			User loginUser = dao.login(user.getEmail(),MD5Util.md5(user.getPassword()));
			if(loginUser != null){
				//1.获取当前时间
				Date date = new Date();
				//获取本机IP
			    dao.updateTimeIp(date.getTime(),request.getRemoteAddr(),loginUser.getId());
System.out.println(request.getRemoteAddr());
			    //用户存在,提示用户的邮箱是否激活如果激活过则进入主页-----------------暂不判断
				session.put("loginUser", loginUser);
				return "to_mainPage";
			}else{
				return "to_login";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public String getEmailAddress() {
		return emailAddress;
	}
	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	
	public boolean isErrorEmailAddress() {
		return errorEmailAddress;
	}
	public void setErrorEmailAddress(boolean errorEmailAddress) {
		this.errorEmailAddress = errorEmailAddress;
	}
	public String getCodeStr() {
		return codeStr;
	}
	public void setCodeStr(String codeStr) {
		this.codeStr = codeStr;
	}
	public String getEmailCode() {
		return emailCode;
	}
	public void setEmailCode(String emailCode) {
		this.emailCode = emailCode;
	}
	public String getErrorInputEmailCode() {
		return errorInputEmailCode;
	}
	public void setErrorInputEmailCode(String errorInputEmailCode) {
		this.errorInputEmailCode = errorInputEmailCode;
	}
}
